﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TigerAndGoats.Framework;
using TigerAndGoats.Resources;
using TigerAndGoats.Sprites;

namespace TigerAndGoats.Control
{
    public class GameControl
    {
        public enum Round {
            Tiger,
            Goats
        }

        public enum GameState {
            InProcess,
            Finished
        }

        private static Round _currentRound = Round.Tiger;
        // 游戏回合从老虎开始
        public static Round currentRound {
            get {
                return _currentRound;
            }
            set
            {
                _currentRound = value;
                _lbl_RoundTip.Text = GetCurrentRoundWithText();
                _btn_GoatGroup.Enabled = _currentRound == Round.Goats && GoatsOnHand > 0;
                if (GoatsOnHand <= 0)
                {
                    _btn_GoatGroup.Text = string.Format(UITipText.GOAT_MOVE_SITUATION, GlobalSettings.MAX_GOATS_STEPS - GoatsSteps);
                }
            }
        }

        private static GameState _currentGameState = GameState.Finished;
        public static GameState CurrentGameState {
            get {
                return _currentGameState;
            } 
            set {
                _currentGameState = value;
                _btn_NewGame.Visible = _currentGameState == GameState.Finished;
            }
        }

        private static Label _lbl_RoundTip;
        // 放羊按钮
        private static Button _btn_GoatGroup;
        public static Button Btn_GoatGroup {
            get {
                return _btn_GoatGroup;
            }
        }

        // 游戏重新开始按钮
        private static Button _btn_NewGame;
        public static Button Btn_NewGame
        {
            get
            {
                return _btn_NewGame;
            }
        }

        private static Label _generalTip;
        public static Label SetGeneralTip(string text) {
            _generalTip.Text = text;
            return _generalTip;
        }

        private static GoatGroup _goatGroup = new GoatGroup();
        public static Tiger TigerA { get; set; }

        // 一只老虎的情况下，除了棋盘上的九只羊，手里还有18只羊，用于直接摆到棋盘上的任意空白点
        private static short _goatsOnHand = GlobalSettings.INIT_GOATS_ONHAND;
        public static short GoatsSteps { get; set; }
        
        public static short GoatsOnHand {
            get {
                return _goatsOnHand;
            } 
            set {
                _goatsOnHand = value;
                _btn_GoatGroup.Text = GetGoatGroupWithText();
            }
        }

        public static Form Main { get; set; } // 会在主窗体创建的时候，将主窗体赋值过来（用它来进行游戏的标题，回合提示，以及羊群的控制）

        private static bool _isChequerInitted = false;
        public static bool IsChequerInitted {
            get
            {
                return _isChequerInitted;
            }
            set
            {
                _isChequerInitted = value;
            }
        }

        public static void InitGame()
        {
            if (Main != null)
            {
                var mainControls = Main.Controls;
                // 添加游戏标题
                var title_Font = new System.Drawing.Font(new FontFamily(UITipText.TITLE_FONT), 25);
                var lbl_GameTitle = new Label() { Text = UITipText.GAME_TITLE, AutoSize = true, Font = title_Font };
                var gameTitleSize = TextRenderer.MeasureText(UITipText.GAME_TITLE, title_Font);
                int gameTitle_X = Main.Size.Width/2 - gameTitleSize.Width / 2;
                lbl_GameTitle.Location = new System.Drawing.Point(gameTitle_X, GlobalSettings.BORDER_WIDTH_V);
                mainControls.Add(lbl_GameTitle);

                // 添加游戏回合提示
                var flp_RoundTip = new FlowLayoutPanel() { Location = new Point(
                    GlobalSettings.BORDER_WIDTH_X / 2
                        , GlobalSettings.BORDER_WIDTH_V * 3 + gameTitleSize.Height - 10)
                    , FlowDirection = FlowDirection.TopDown};

                var lbl_RoundTipTitle_Font = new System.Drawing.Font(new FontFamily(UITipText.ROUND_TIP_TITLE_FONT), 22);
                var roundTipTitle = UITipText.CURRENT_ROUND;
                var lbl_RoundTipTitle = new Label() { Text = roundTipTitle, AutoSize = true, Font = lbl_RoundTipTitle_Font };
                var roundTipTitleSize = TextRenderer.MeasureText(roundTipTitle, lbl_RoundTipTitle_Font);
                flp_RoundTip.Controls.Add(lbl_RoundTipTitle);

                var lbl_RoundTip_Font = new System.Drawing.Font(new FontFamily(UITipText.ROUND_TIP_FONT), 20);
                var roundTip = GetCurrentRoundWithText();
                _lbl_RoundTip = new Label() { Text = roundTip, AutoSize = true, Font = lbl_RoundTip_Font, ForeColor = Color.Red };
                //var roundTipSize = TextRenderer.MeasureText(GlobalSettings.GameTitle, lbl_RoundTip_Font);
                int roundTip_X = GlobalSettings.BORDER_WIDTH_V * 2 + (lbl_RoundTipTitle.Width / 2 - roundTipTitleSize.Width / 2);
                int roundTip_Y = GlobalSettings.BORDER_WIDTH_V * 2 + roundTipTitleSize.Height;
                flp_RoundTip.Controls.Add(_lbl_RoundTip);
                mainControls.Add(flp_RoundTip);

                // 添加羊群
                var goatGroupFont = new System.Drawing.Font(new FontFamily(UITipText.GOAT_GROUP_FONT), 22);
                var goatGroupText = GetGoatGroupWithText();
                _btn_GoatGroup = new Button() { Text = goatGroupText, AutoSize = true, Font = goatGroupFont };
                var btnGoatGroupSize = TextRenderer.MeasureText(goatGroupText, goatGroupFont);
                _btn_GoatGroup.Location = new Point(roundTip_X + roundTipTitleSize.Width + GlobalSettings.BORDER_WIDTH_X + 70, roundTip_Y);
                mainControls.Add(_btn_GoatGroup);

                _btn_GoatGroup.Click += (object? sender, EventArgs e) => {
                    if (_goatGroup.IsPutup)
                    {
                        _goatGroup.PutDown();
                    }
                    else
                    {
                        _goatGroup.PutUp();
                    }
                };

                var flp_BottomGenelTipAndBtn = new FlowLayoutPanel() {
                    Size = new Size(GlobalSettings.STEP_LENGTH * GlobalSettings.HORIZONTAL_STEPS
                    , GlobalSettings.STEP_LENGTH * 2),
                    AutoSizeMode = AutoSizeMode.GrowAndShrink,
                    AutoScroll = true,
                    Location = new Point(GlobalSettings.BORDER_WIDTH_V
                    , GlobalSettings.BORDER_WIDTH_V * 3 + ChequerGraph.GetRightBottomPoint().Y
                    ) 
                };

                // 添加游戏普通提示
                var generalTipFont = new System.Drawing.Font(new FontFamily(UITipText.GENERAL_TIP_FONT), 15);
                _generalTip = new Label() { Text = UITipText.GAME_START_CLICK_TIP, AutoSize = true, Font = generalTipFont };

                // 添加游戏开始按钮
                _btn_NewGame = new Button() {
                    Text = UITipText.GAME_START,
                    Font = new System.Drawing.Font(new FontFamily(UITipText.NEW_GAME_BTN_FONT), 20),
                    AutoSize = true, 
                    Visible = true, 
                    Margin = new Padding(5),
                };

                _btn_NewGame.Click += (object? sender, EventArgs e) => {
                    NewGame();
                };

                flp_BottomGenelTipAndBtn.Controls.Add(_generalTip);
                flp_BottomGenelTipAndBtn.Controls.Add(_btn_NewGame);
                mainControls.Add(flp_BottomGenelTipAndBtn);

                // 开始游戏（本来游戏已经是老虎开始了，这里就是加一个提示，告知玩家此时需要操作老虎开始下棋） 
                _btn_GoatGroup.Enabled = false;
            }
        }

        public static void WinGame(string winTextTip)
        {
            GameControl.CurrentGameState = GameControl.GameState.Finished;
            GameControl.SetGeneralTip(winTextTip);
            _btn_NewGame.Text = UITipText.GAME_RESTART;
        }

        public static void NewGame()
        {
            GameControl.CurrentGameState = GameControl.GameState.InProcess;
            // 控制游戏重新开始
            _isChequerInitted = false;
            _goatsOnHand = GlobalSettings.INIT_GOATS_ONHAND;
            GoatsSteps = 0;
            _generalTip.Text = UITipText.GAME_START_INTRODUCTION;
            ChequerGraph.RefreshUI(); // 刷新让游戏重置生效
            // 记录胜负结果
        }

        private static string GetGoatGroupWithText()
        {
            return string.Format(UITipText.GOAT_GROUP_SITUATION, GoatsOnHand);
        }

        private static string GetCurrentRoundWithText()
        {
            switch (currentRound)
            {
                case Round.Tiger:
                    return UITipText.TIGER;
                case Round.Goats:
                    return UITipText.GOAT;
                default:
                    return string.Empty;
            }
        }
    }
}
